<?php
global $AppUI, $m;

//
$uid = $AppUI->user_id;
$perms =& $AppUI->acl();
$canEdit = $perms->checkModuleItem('quotations', 'edit');
if (!$canEdit) {
	$results['success'] = FALSE;
	$results['messages'] = 'You do not have permission to delete quotation!';
}
else {
	// create a new instance of the einstein class
	$obj = new CQuotations();
	
	// detect if a delete operation has to be processed
	$del = dPgetParam($_POST, 'del', 0);
	
	$quote_id = intval(isset($_POST['quote_id']) ? $_POST['quote_id']: $_GET['quote_id']);
	
	if($del){
		// Delete Quotation
		if(!$quote_id) {
			$results['success'] = FALSE;
			$results['messages'] = 'You do not select any quotation to delete';
		}
		else{
			$obj->load($quote_id);
			
			require_once($AppUI->getMasterDataClass('mdstatus'));
			$proposed_key = dPgetSysVal(PROPOSED_KEY);
			$objMD = new CMdStatus();
			$objMD->loadByCode($proposed_key['code']);
			
			if ($obj->quote_status != $objMD->status_id) {
				$results['success'] = FALSE;
				$results['messages'] = 'You can only delete a proposed quotation!';
			}
			else if ($msg = $obj->delete($quote_id)){
				$results['success'] = FALSE;
				$results['messages'] = $msg;
			}else{
				$results['success'] = TRUE;
			}
		}
	} else {
		$results['success'] = FALSE;
		$results['messages'] = 'Action is not permit';
	}
}

header('Content-Type: text/xml');

$xml = new XmlWriter();
$xml->openMemory();
$xml->startDocument('1.0', 'UTF-8');

$xml->startElement('root');

$xml->writeElement('quote_id', $obj->quote_id);
$xml->writeElement('quote_code', $obj->quote_code);
$xml->writeElement('success', $results['success'] ? 'true' : 'false');
$xml->writeElement('errors', addslashes(htmlentities($results['messages'])));
$xml->endElement();

echo $xml->outputMemory(true);
?>
